package com.skyable.account.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.skyable.account.entity.SysDept;
import com.skyable.account.service.IDeptService;
import com.skyable.common.common.ResponseResult;
import com.skyable.common.common.ServiceResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author GaoYuan
 * @title: SysDeptController
 * @projectName spring-cloud-oauth2-demo
 * @description: TODO
 * @date 2021/4/1717:24
 */
@Api(value = "部门管理")
@RestController
@RequestMapping("dept")
public class DeptController extends BaseController {

    @Autowired
    private IDeptService sysDeptService;

    /**
     * 新增部门信息
     * @param sysDept
     * @return
     */
    @ApiOperation(value = "新增部门信息")
    @PostMapping("/insert")
    public ResponseResult insertDept(@RequestBody SysDept sysDept, HttpServletRequest request) {
        setInsertInfo(sysDept,request);
        ServiceResult<SysDept> serviceResult = sysDeptService.insertDept(sysDept);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }

        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 更新部门信息
     * @param sysDept
     * @return
     */
    @ApiOperation(value = "更新部门信息")
    @PutMapping("/update")
    public ResponseResult updateDept(@RequestBody SysDept sysDept, HttpServletRequest request) {
        setUpdateInfo(sysDept,request);
        ServiceResult<SysDept> serviceResult = sysDeptService.updateDept(sysDept);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }

        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 获取部门信息列表
     * @return
     */
    @ApiOperation(value = "获取部门信息列表")
    @GetMapping("/view")
    public ResponseResult sysDeptList(){
        ServiceResult<List<SysDept>> serviceResult = sysDeptService.findDeptList();
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 根据ID查询部门信息
     * @param deptId
     * @return
     */
    @ApiOperation(value = "根据ID查询部门信息")
    @GetMapping("/view/{id}")
    public ResponseResult getDeptById(@PathVariable("id") Long deptId){
        ServiceResult<SysDept> serviceResult = sysDeptService.findDeptById(deptId);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 分页查询部门列表
     * @param page
     * @param limit
     * @param sysDept
     * @return
     */
    @ApiOperation(value = "分页查询部门列表")
    @PostMapping("/view/{page}/{limit}")
    public ResponseResult sysDeptList(@PathVariable("page")Integer page, @PathVariable("limit")Integer limit, @RequestBody SysDept sysDept){
        ServiceResult<IPage<SysDept>> serviceResult = sysDeptService.findDeptListByPage(page,limit,sysDept);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 删除部门信息
     * @param deptId
     * @return
     */
    @ApiOperation(value = "删除部门信息")
    @DeleteMapping("/delete/{id}")
    public ResponseResult deletePermission(@PathVariable("id") Long deptId){
        ServiceResult<Integer> serviceResult = sysDeptService.deleteDeptById(deptId);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult());
    }
}
